package com.agenda.daoHibernate;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import util.HibernateUtil;

import com.agenda.dao.LogDao;
import com.agenda.entity.Log;

public class LogDaoHibernate implements LogDao {

	private Session sessao;
	
	@Override
	public void save(Log log) {
		this.sessao = HibernateUtil.getSession().getCurrentSession();
		this.sessao.save(log);		
	}

	public Session getSessao() {
		return sessao;
	}

	public void setSessao(Session sessao) {
		this.sessao = sessao;
	}
	
	@Override
	public List list() {
		try {
			this.sessao = HibernateUtil.getSession().getCurrentSession();
			List lista = (List<Log>) this.sessao.createQuery("From Log ").list();		
		
			return lista;
		} catch (Exception e) {
			System.out.println("Erro no DaoHibernate: " + e);
		}

		return null;
	}
	

	public List<Log> findByDate(Log log, Date dtInicial, Date dtFinal) {
		this.sessao =  HibernateUtil.getSession().getCurrentSession();
//		Example example = Example.create(log);
//		example.enableLike(MatchMode.START);
//		example.ignoreCase();
//		List listaLog = this.sessao.createCriteria(Log.class).add(example).list();
//		this.sessao.beginTransaction().commit();
//		this.sessao.close();
//		return listaLog;
		
		if((log.getUsuario().getIdUsuario() != null) && (log.getUsuario().getIdUsuario() != 0)) { 
			try {	
				List<Log> lista = this.sessao
                    .createCriteria(Log.class)
                    .add(Restrictions.eq("projeto", log.getProjeto()))
                    .add(Restrictions.eq("usuario", log.getUsuario()))
                    .add(Restrictions.between("data", dtInicial, dtFinal))                    
                    .list(); 
				return lista;
		} catch (Exception e) {
			return null;
		}
		} else  {
			try {	
				List<Log> lista = this.sessao
                    .createCriteria(Log.class)
                    .add(Restrictions.eq("projeto", log.getProjeto()))
                    .add(Restrictions.between("data", dtInicial, dtFinal))
                    .list(); 
				return lista;
		} catch (Exception e) {
			return null;
		}
		}

	}
	
	public List<Log> findByUser(Log log) {	
		this.sessao =  HibernateUtil.getSession().getCurrentSession();
		try {	
			List<Log> lista = this.sessao
				.createCriteria(Log.class)
				.add(Restrictions.eq("usuario", log.getUsuario()))
				.add(Restrictions.eq("projeto", log.getProjeto()))
				
				.list(); 
			return lista;
		} catch (Exception e) {
			return null;
		}		
	}
	
	@Override
	public List<Log> findByPrimaryKey(Log log) {
		Log auxLog = log;
		Integer idLog = log.getIdLog();
		this.sessao = HibernateUtil.getSession().getCurrentSession();
		String hql = "from Log p where p.idLog = :idLog";
		Query select = this.sessao.createQuery(hql);
		select.setInteger("idLog", idLog);
		List listaLog = select.list();
		return listaLog;
	}

	public void delete(Log log) {
		this.sessao.delete(log);
	}
	
}
